package org.springframework.prospring.ticket.web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.prospring.ticket.service.RegionManager;
import org.springframework.prospring.ticket.service.ClubManager;
import org.springframework.prospring.ticket.service.UserManager;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.domain.ui.Response;
import org.springframework.prospring.ticket.web.UserContext;
import org.springframework.prospring.ticket.web.views.JsonView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserLoginController {
    RegionManager regionManager;
    ClubManager clubManager;
    UserManager userManager;

    public RegionManager getRegionManager() {
        System.out.println("===================== UserLoginController.getRegionManager");
        return regionManager;
    }

    public void setRegionManager(RegionManager regionManager) {
        System.out.println("===================== UserLoginController.setRegionManager");
        this.regionManager = regionManager;
    }

    public ClubManager getClubManager() {
        System.out.println("===================== UserLoginController.getClubManager");
        return clubManager;
    }

    public void setClubManager(ClubManager clubManager) {
        System.out.println("===================== UserLoginController.setClubManager");
        this.clubManager = clubManager;
    }

    public UserManager getUserManager() {
        System.out.println("===================== UserLoginController.getUserManager");
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        System.out.println("===================== UserLoginController.setUserManager");
        this.userManager = userManager;
    }

    @RequestMapping("/checkUserLogin.dahtml")
    public ModelAndView checkUserLogin(HttpServletRequest req, HttpServletResponse res, HttpSession session, String email, String password) {
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.UserLoginController.checkUserLogin req = " + req+", email="+email+", password="+password);

        HttpSession mySession = req.getSession();
        System.out.println("=========== mySession.getId() = " + mySession.getId());
        System.out.println("=========== session.getId() = " + session.getId());
        UserContext userContext = (UserContext) session.getAttribute("userContext");
        if (userContext == null){
            userContext = new UserContext();
        }
        userContext.setNickname(email);
        userContext.setFirstName(password);
        req.getSession().setAttribute("userContext", userContext);

        Response response = new Response();
        response.setDataObject(userContext);
        ModelAndView mav = new ModelAndView(JsonView.instance);
        mav.addObject(JsonView.JSON_ROOT, response);
        //mav.addObject(JsonView.JSON_ROOT, userContext);
        mav.addObject("responseStatus", "OK");
        mav.addObject("message", "este es un mensaje");
        mav.addObject("nickname", userContext.getNickname());
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.UserLoginController.checkUserLogin  ModelAndView = " + mav);
    	return mav;

    }


}
